package com.rms.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.rms.domain.Admin;
import com.rms.utils.servlet.BaseServlet;

@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static final String SESSION_NAME = "admin";

	/**
	 * 封装表单数据
	 * 
	 * @param req
	 * @return
	 * @author Zheng weiyu 2019年7月29日
	 */
	private Admin toBean(HttpServletRequest req) {
		Admin admin = new Admin();
		try {
			BeanUtils.populate(admin, req.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admin;
	}
//在BaseServlet中实现
//	public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		/**
//		 * 1.得到表单数据，封装成javaBean 2.调用service的login方法 3.登陆成功,保存用户到session域，转发到main.jsp
//		 * 登录失败转发到login.jsp
//		 */
//		// 封装表单数据
//		Admin admin = toBean(req);
//		// 调用service的login方法
//		try {
//			Admin w = adminService.login(admin,Admin.class);
//			req.getSession().setAttribute(SESSION_NAME, w);
//			// 执行成功,跳转到main.jsp
//			req.getRequestDispatcher("admin.jsp").forward(req, resp);
//		} catch (Exception e) {
//			// 登录失败，保存错误信息并跳转到login.jsp
//			e.printStackTrace();
//			req.setAttribute("message", "登录失败!" + e.getMessage());
//			req.getRequestDispatcher("login.jsp").forward(req, resp);
//		}
//	}

	public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.移除session域的user 2.成功转发到login.jsp
		 */
		try {
			req.getSession().removeAttribute(SESSION_NAME);
			req.setAttribute("message", "退出成功!");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void deleteCompany(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		int company_id = Integer.parseInt(req.getParameter("company_id"));
		try {
			adminService.deleteCompany(company_id);
			req.setAttribute("mesage", "删除成功");
		}catch(Exception e) {
			req.setAttribute("message", "删除失败");
		}
		req.getRequestDispatcher("ToCompanyAdminPageServlet").forward(req, resp);
	}
	
	public void deleteHR(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		int hr_id = Integer.parseInt(req.getParameter("hr_id"));
		try {
			adminService.deleteHrById(hr_id);
			req.setAttribute("mesage", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("message", "删除失败");
		}
		req.getRequestDispatcher("ToAllHRsAdminServlet").forward(req, resp);
	}
	
	public void deleteWorker(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		int worker_id = Integer.parseInt(req.getParameter("worker_id"));
		try {
			adminService.deleteWorkerById(worker_id);
			req.setAttribute("mesage", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("message", "删除失败");
		}
		req.getRequestDispatcher("ToAllWorkerAdminPageServlet").forward(req, resp);
	}
	
}
